home *** CD-ROM | disk | FTP | other *** search
/ The Word 5 / The Word 5.adf / FiLeS / ARexx2.txt / ARexx2.txt
Text File  |  2022-11-05  |  6KB  |  234 lines

  1. |How To Program In ARexx - Part 2
  2.  
  3.  
  4.  
  5.                            By CROW of The LOC
  6.  
  7. Have  you  read part 1 of this series, if you have no idea about ARexx I
  8. would  strongly  recommend  that  you do.  In this article I will try to
  9. cover how to create, read and write files and also how to create menus.
  10.  
  11. With  all  file operations the open function/procedure is used, the open
  12. has the following structure :-
  13.  
  14. boolean = Open(file, name, [Mode])
  15.  
  16. file = reference variable for that file.
  17. name = is the path and filename.
  18. mode = what the user wants to do with the file.
  19. Mode values are R = Read (default), W = Write, A = Append.
  20.  
  21. If everything went ok TRUE is returned otherwise FALSE is returned.
  22.  
  23.  
  24. Creating  a  file  -  this example also tells the user if something went
  25. wrong.   If  everything  went  ok,  then  you  should have a file called
  26. tempfile in your RAM:  disk with a size of zero bytes.
  27.  
  28. /* Example 11 */
  29.  
  30. If Open("tempfile", "RAM:Tempfile", "W") Then
  31.    Call Close("tempfile"
  32. Else
  33.    Say "Unable To Create File, in RAM:"
  34.  
  35.  
  36. Creating and writing to a file - this time more flexible with constants.
  37. NOTE  :-  to  signify  a  block  use  Do then End, (BEGIN END for Pascal
  38. experts!),  if  you  want  more than one statement to be run following a
  39. condition  then  you have to put a Do and an End Around them or you will
  40. get an error or weird results.
  41.  
  42. /* Example 12 */
  43.  
  44. fileptr = "tempfile"
  45. path    = "RAM:TempFile"
  46.  
  47. If Open(fileptr, path, "W") Then Do   /* Here is A DO */
  48.    Call Writeln(fileptr, "This is Line 1")
  49.    Call Writeln(fileptr, "This is Line 2")
  50.    Call Writeln(fileptr, "This is Line 3")
  51.    Call Close(fileptr)
  52. End                                   /* Here is A END */
  53. Else
  54.    Say "Unable to create and write file..."
  55.  
  56.  
  57. Opening a file and reading the contents.
  58.  
  59. /*   Example 13    */
  60.  
  61. fileptr = "tempfile"
  62. path    = "RAM:TempFile"
  63.  
  64. If Open(fileptr, path, "R") Then Do
  65.      Do While ~Eof(fileptr)    /* While NOT End Of File */
  66.         Say Readln(fileptr)   /* Read A Line Of Characters */
  67.      End
  68.      Call Close(fileptr)
  69. End
  70. Else
  71.    Say "Unable to open and read file..."
  72.  
  73.  
  74. Creating  a  menu  with validation so invalid options are not processed,
  75. also shows how to write procedures.
  76.  
  77. /* Example 14 */
  78.  
  79. CR = '0A'x               /* CR = Carriage Return */
  80. FF = '0C'x               /* FF =  FormFeed (Clear Screen) */
  81. ClearScreen = FF || CR   /* || = concatenate : means Join together */
  82.  
  83. Do Forever
  84.     Say ClearScreen
  85.  
  86.     Say " (A) - Archive"
  87.     Say " (B) - Batch"
  88.     Say " (C) - Catch"
  89.     Say ""
  90.     Say " (X) - Exit"
  91.     Say ""
  92.  
  93.     Options Prompt "Enter Choice : "; pull choice 2
  94.  
  95.     Choice = Upper(Chocie)
  96.  
  97.     If Choice = "X" Then Do     /* This is where we Quit */
  98.        Say "Exiting..."
  99.        Exit
  100.     End
  101.     Else If Choice = "A" Then Do
  102.        Say "Archive"
  103.        Call WaitForCR()
  104.     End
  105.     Else If Choice = "B" Then Do
  106.        Say "Batch"
  107.        Call WaitForCR()
  108.     End
  109.     Else If Choice = "C" Then Do
  110.        Say "Catch"
  111.        Call WaitForCR()
  112.     End
  113. End
  114.  
  115. WaitForCR(): Procedure
  116.     Say ""
  117.     Options Prompt "Return to Continue"
  118.     Do Until C==""
  119.        Parse Pull c
  120.     End
  121. Return
  122.  
  123. NOTE  :   You  may have noticed that when you use the function Say, that
  124. the cursor goes onto the next line this is not very useful when you have
  125. something  like  (run  example  1  if  you  still  do not know what i am
  126. wittering about ) :-
  127.  
  128.      Say "Enter Your Name : "
  129.      Parse Pull name
  130.  
  131. So to solve this little quirk, use the following instead :-
  132.  
  133.      Options Prompt "Enter Your Name : "
  134.      Parse Pull name
  135.  
  136. Now  all  you have to do is then redefine the prompt to what you want it
  137. do be after this piece of code.
  138.  
  139.                        Procedures and Functions.
  140.  
  141. In  this  part  of the article I will try to cover how to create and use
  142. procedures  and  functions.   The  difference  between  a function and a
  143. procedure,  is  that  a  function  is something whereas a procedure does
  144. something.   Please  note  that  this definition does not cover all high
  145. level  language  implementations  of  functions  and procedures.In ARexx
  146. procedure  and functions seem to merge so that you can write a procedure
  147. and function to do the same thing.
  148.  
  149. This  is  a  very  simple  example of how to define and use a procedure,
  150. variables  defined  in  the  procedure are only visible to the procedure
  151. (there  are  exceptions, that will be covered later).  And variables and
  152. constants defined outside of the procedure are normally not visible to a
  153. procedure.
  154.  
  155. /* Example 15 */
  156.  
  157. Call PrettyLine()
  158. Exit
  159.  
  160. PrettyLine: Procedure
  161.  
  162.      Say "/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\"
  163.  
  164. Return
  165.  
  166.  
  167. This  example  shows  the  most basic example of passing an actual value
  168. back with the return statement, this is a function :-
  169.  
  170. /* Example 16 */
  171.  
  172. Say PI()
  173. Exit
  174.  
  175. PI:                 /* This Is A Function */
  176. Return 3.14159
  177.  
  178.  
  179. Now  lets  show you how to pass values into a procedure, by altering the
  180. PrettyLine  Procedure  in  Example 15 so we can specify type of char and
  181. number of chars to print :-
  182.  
  183. /* Example 17 */
  184.  
  185. Say PrintLine("*", 20)     /*  PrintLine( CharString, NoOfChars ) */
  186. Exit
  187.  
  188.  
  189. PrintLine:              /* This Is A Function */
  190.        character = arg(1)  /* Get Character To Print */
  191.        number    = arg(2)  /* Get Number Of Chars */
  192.        line      = ""      /* Initialise Line */
  193.        Do Number
  194.           line = line || Character  /* Make Line Of What Ever Char Is */
  195.        End
  196.  
  197. Return line
  198.  
  199. The  above  example  shows  how to pass values to a function, it is quit
  200. flexible,  as  a  whole string eg.  "Hello" can be passed and not just a
  201. single  character.   The next example takes this slightly further by now
  202. adding  another parameter so that we now can draw a rectangle instead of
  203. just a single line.
  204.  
  205. /* Example 18 */
  206.  
  207. Say PrintLine("*", 20, 10) /* PrintLine(CharString,NoOfChars,NoLines) */
  208. Exit
  209.  
  210.  
  211. PrintLine:              /* This Is A Function */
  212.  
  213.           CR = "0A"x    /* Carriage Return */
  214.  
  215.     character = arg(1)  /* Get Character To Print */
  216.     number    = arg(2)  /* Get Number Of Chars */
  217.     nolines   = arg(3)  /* Get Number Of Lines */
  218.     line      = ""      /* Initialise Line */
  219.     Do number
  220.        line = line || Character   /* Make Line Of What Ever Char Is */
  221.     End
  222.  
  223.     box = ""            /* Initialise box */
  224.     Do nolines
  225.        box = box || line || CR
  226.     End
  227.  
  228. Return box
  229.  
  230. The  above  example  (18)  uses  the  || symbol which is the concatenate
  231. symbol which is used to join two or more strings together.
  232.  
  233. end.
  234.